/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.meuevento.business.user;

import com.meuevento.business.event.Event;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.Cacheable;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

/**
 *
 * @author Fernando Arena Varella
 */
@Entity
@Table(name="users")
@Cacheable(false)
@NamedQueries({
    @NamedQuery(name=User.FIND_ALL, query="SELECT u FROM User u")
})
public class User implements Serializable {
    public static final String FIND_BY_NAME = "User.findByName";
    public static final String FIND_ALL = "User.findAll";

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long userId;

    @Column
    private String username;
    @Column
    private String password;

//    @Pattern(regexp="(\\s)*@(\\s)*(.(\\s*))*",
//        message="formato de email inválido")
    @Column
    private String email;

    @ManyToMany(mappedBy="users", cascade=CascadeType.ALL)
//    @JoinTable(
//        name="user_event",
//        joinColumns=@JoinColumn(name="userId"),
//        inverseJoinColumns=@JoinColumn(name="eventId"))
    private List<Event> events;

    public User(String username, String password, String email, List<Event> events) {
        this.username = username;
        this.password = password;
        this.email = email;
        this.events = events;
    }


    public User(){
        
    }


    public void addEvent(Event event){
        if (events == null)
            events = new ArrayList<Event>();
        events.add(event);
    }

    public Long getUserId() {
        return userId;
    }

    public void setUserId(Long id) {
        this.userId = id;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public List<Event> getEvents() {
        return events;
    }

    public void setEvents(List<Event> events) {
        this.events = events;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }






    @Override
    public int hashCode() {
        int hash = 0;
        hash += (userId != null ? userId.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof User)) {
            return false;
        }
        User other = (User) object;
        if ((this.userId == null && other.userId != null) || (this.userId != null && !this.userId.equals(other.userId))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.meuevento.business.user.User[id=" + userId + "]";
    }

}
